package com.gsm.projects.jobDw.mock;

import com.github.javafaker.Faker;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Timestamp;
import java.util.Locale;
import java.util.Random;

public class JobRecordGenerator {

    private static final Faker faker = new Faker(new Locale("zh_CN"));
    private static final Random random = new Random();

    private static final String URL = "jdbc:mysql://hadoop102:3306/db_job_realtime_statistics";
    private static final String USER = "root";
    private static final String PASSWORD = "123456";

    private static void insertJobRecord(Connection conn) throws Exception {
        String sql = "INSERT INTO t_job (name, city, school, type, major, graduation_date, company, position, salary, received_offer_time, entry_date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
            // 生成随机但真实的数据
            pstmt.setString(1, faker.name().fullName());
            pstmt.setString(2, faker.address().city());
            pstmt.setString(3, faker.university().name());
            pstmt.setString(4, "本科");
            pstmt.setString(5, faker.options().option("计算机科学", "软件工程", "电子信息工程", "机械工程", "土木工程", "经济学", "管理学", "法学", "文学", "艺术设计"));
            pstmt.setDate(6, new java.sql.Date(System.currentTimeMillis() - random.nextInt(365 * 24 * 60 * 60 * 1000)));
            pstmt.setString(7, faker.company().name());
            pstmt.setString(8, faker.job().title());
            pstmt.setBigDecimal(9, new java.math.BigDecimal(random.nextDouble() * 100000 + 5000));
            pstmt.setTimestamp(10, new Timestamp(System.currentTimeMillis() - random.nextInt(365 * 24 * 60 * 60 * 1000)));
            pstmt.setTimestamp(11, new Timestamp(System.currentTimeMillis() - random.nextInt(365 * 24 * 60 * 60 * 1000)));

            pstmt.executeUpdate();
        }
    }

    public static void main(String[] args) {
        try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD)) {
            while (true) {
                Thread.sleep(1000); // 每秒执行一次
                insertJobRecord(conn);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}